package 线程和TCP网络编程.TAKS2.ServerSocket;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 服务器端的创建
 *
 * @author 君未洋
 */
public class ServerTest {
    public static List<Socket> socketList = new ArrayList<>();

    public static void main(String[] args) throws Exception {
        ServerSocket serverSocket = new ServerSocket(9999);
        ExecutorService es = Executors.newCachedThreadPool();
        //创建线程池对象
        System.out.println("聊天室开启");
        while (true) {
            Socket socket = serverSocket.accept();
            //从请求连接的队列中获取客户端的socket对象
            System.out.println("上线通知： 用户" + socket.getPort() + "上线啦！");
            socketList.add(socket);
            //将获取到的客户端对象放入到集合中,用于后续的通知用
            es.submit(new ServerRunnableImpl(socket));
            //创建线程任务,把任务放入到线程中
        }
    }
}



